﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using AppUtility;

namespace UIDocument
    {
    /// <summary>
    /// Summary description for FileUploder
    /// </summary>
    public class FileUploder : IHttpHandler
        {
        public string FileName { set ; get; }
        public void ProcessRequest(HttpContext context)
            {

            try
                {
                if (HttpContext.Current.Request.Files[0] == null)
                    {
                    throw new Exception();
                    }
                HttpPostedFile postedFile = context.Request.Files[0];
                string filename1 = DateTime.Now.ToString("dd/MM/yyyy h:mm:ss");
                filename1 = filename1.Replace("/", "").Replace(":", "").Replace(" ", "");
                filename1 = filename1 + "" + HttpContext.Current.Request.Files[0].FileName;

                if (filename1.Length > 0)
                    {

                    switch (Path.GetExtension(filename1))
                        {
                        case ".jpg":
                        case ".jpeg":
                            postedFile.SaveAs(context.Server.MapPath("~/Image/UploadImage/"+filename1));
                           // context.Request.Files["form"].SaveAs(context.Server.MapPath("Image/UploadImage/"+filename1)); 
                            this.FileName = filename1;
                            break;
                        case ".exls":
                            context.Request.Files["form"].SaveAs(context.Server.MapPath("~/DocumentFile/Excel/"+filename1));
                            this.FileName = filename1;
                            break;
                        case ".pdf":
                            context.Request.Files["form"].SaveAs(context.Server.MapPath("~/DocumentFile/PdfFile/"+filename1));
                            this.FileName = filename1;
                            
                            break;
                        case ".doc":
                            context.Request.Files["form"].SaveAs(context.Server.MapPath("~/DocumentFile/WordFile/"+filename1));
                            this.FileName = filename1;
                            break;
                        default:
                            throw new Exception("File Extenction not valid !!!");
                        }
                    context.Response.Write(this.FileName);  
                        
                    }                                                       

                }
            catch(Exception ex)
                {
                new JsonResponse() { Data =null, StatusCode = JsonResponse.Status.Failed, ErrorMessage =ex.Message};
                }
            }

        public bool IsReusable
            {
            get
                {
                return true;
                }
            }
        }
    }